﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Tb.App.API.Applications.Command.DeepProcessing;
using Tb.App.Domain.Rabbit.DeepProcessing;
using Tb.App.ErrorCode;
using Tb.App.Infrastructure.Interfaces;

namespace Tb.App.API.Applications.CommandHandler.DeepProcessing
{
    /// <summary>
    /// 深加工企业信息查询--处理
    /// </summary>
    public class GetDeepProcessingCompanyQueryCommandHandler : IRequestHandler<GetDeepProcessingCompanyQueryCommand, ApiResult<DeepProcessingCompany>>
    {
        private readonly IBaseRepository<DeepProcessingCompany> deeprocessingCompanyRep;

        public GetDeepProcessingCompanyQueryCommandHandler(IBaseRepository<DeepProcessingCompany> deeprocessingCompanyRep)
        {
            this.deeprocessingCompanyRep = deeprocessingCompanyRep;
        }
        public async Task<ApiResult<DeepProcessingCompany>> Handle(GetDeepProcessingCompanyQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<DeepProcessingCompany> result = new ApiResult<DeepProcessingCompany>();
            var list = await deeprocessingCompanyRep.GetAll().Where(x => !x.IsDel && x.Batch == request.Batch).FirstOrDefaultAsync();
            if (list == null)
            {
                result.Code = ApiEnum.查询失败;
                result.Message = "无数据";
                result.Data = null;
            }
            else
            {
                result.Data = list;
                result.Message = "查询成功";
                result.Code = ApiEnum.查询成功;
            }
            return result;
        }
    }
}
